package com.iot08.api.service;

import com.iot08.common.entity.admin.RefundApply;
import com.iot08.common.entity.app.AliPayNotifyInfo;
import com.iot08.common.entity.app.WechatPayNotifyInfo;
import com.iot08.common.service.base.BaseAdminService;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * @author cwj
 * @description
 * @date 2025/2/8 10:27
 **/
@Slf4j
@Service
public class RefundApplyService extends BaseAdminService<RefundApply> {

    
    /**
     * @description 更新退款订单信息
     * @author cwj
     * @param [aliPayNotifyInfo]
     * @return void
     * @date @time 2025/2/8 11:03
     **/
    public void updateRefundApplyByAliPayNotify(AliPayNotifyInfo aliPayNotifyInfo,String status){
         //查询在进行退款中的退款申请记录
         RefundApply refundApply = this.findOne(new Query(Criteria.where("outTradeNo").is(aliPayNotifyInfo.getOutTradeNo()).and("status").is("PROCESSING")).limit(1));
         if(!ObjectUtils.isEmpty(refundApply)){
               Update update = new Update();
               update.set("status",status);
               if("SUCCESS".equals(status)) {
                   if(!ObjectUtils.isEmpty(aliPayNotifyInfo.getRefundFee())){
                       //总退款金额
                       update.set("refund",aliPayNotifyInfo.getRefundFee());
                   }
                   if(!ObjectUtils.isEmpty(aliPayNotifyInfo.getTotalAmount())){
                       //订单总金额
                       update.set("total",aliPayNotifyInfo.getTotalAmount());
                   }
                   if(!ObjectUtils.isEmpty(aliPayNotifyInfo.getSendBackFee())){
                       //商户实际退款金额
                       update.set("sellerRealRefund",aliPayNotifyInfo.getSendBackFee());
                   }
                   if(!ObjectUtils.isEmpty(aliPayNotifyInfo.getBuyerPayAmount())){
                       //用户实际付款金额
                       update.set("payerTotal",aliPayNotifyInfo.getBuyerPayAmount());
                   }
               }
               update.set("updateTime",new Date());
               this.updateById(new ObjectId(refundApply.getId()),update);
         } else {
             log.info("退款记录不存在或已经退款成功，通知退款的订单id为：{}",aliPayNotifyInfo.getOutTradeNo());
         }
    }

    /**
     * @description 更新退款申请信息
     * @author cwj
     * @param [wechatPayNotifyInfo, status]
     * @return void
     * @date @time 2025/2/10 10:41
     **/
    public void updateRefundApplyByWechatNotify(WechatPayNotifyInfo wechatPayNotifyInfo, String status){
        //查询在进行退款中的退款申请记录
        RefundApply refundApply = this.findOne(new Query(Criteria.where("outTradeNo").is(wechatPayNotifyInfo.getOutTradeNo()).and("status").is("PROCESSING")).limit(1));
        if(!ObjectUtils.isEmpty(refundApply)){
            Update update = new Update();
            update.set("status",status);
            if("SUCCESS".equals(status)) {
                if(!ObjectUtils.isEmpty(wechatPayNotifyInfo.getRefund())){
                    //总退款金额
                    update.set("refund",wechatPayNotifyInfo.getRefund());
                }
                if(!ObjectUtils.isEmpty(wechatPayNotifyInfo.getTotal())){
                    //订单总金额
                    update.set("total",wechatPayNotifyInfo.getTotal());
                }
                if(!ObjectUtils.isEmpty(wechatPayNotifyInfo.getPayerTotal())){
                    //用户实际付款金额
                    update.set("payerTotal",wechatPayNotifyInfo.getPayerTotal());
                }
                if(!ObjectUtils.isEmpty(wechatPayNotifyInfo.getPayerRefund())){
                    //用户实际付款金额
                    update.set("payerRefund",wechatPayNotifyInfo.getPayerRefund());
                }
            }
            update.set("updateTime",new Date());
            this.updateById(new ObjectId(refundApply.getId()),update);
        } else {
            log.info("退款记录不存在或已经退款成功，通知退款的订单id为：{}",wechatPayNotifyInfo.getOutTradeNo());
            /*refundApply = this.findOne(new Query(Criteria.where("outTradeNo").is(wechatPayNotifyInfo.getOutTradeNo()).and("status").is("SUCCESS")).limit(1));
            if(!ObjectUtils.isEmpty(refundApply)){
                beforeHasrRefund = true;
            }*/
        }
    }
}
